/**
 * @Author yinyunqi
 * @date 2018年1月30日
 * @Content 
 */
package com.yongqi.controller.back;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yongqi.model.back.Email;
import com.yongqi.pub.DateFormat;
import com.yongqi.pub.EmailHelper;
import com.yongqi.pub.PrincipalFormat;
import com.yongqi.service.back.EmailService;

@Controller
@RequestMapping("/back/email")
public class BackEmailController {
    @Autowired
    EmailService emailService;
    /**
     * 后台邮件管理
     * @return 邮件主页
     */
    @RequestMapping("/index")
    public String backEmailIndex() {
	return "back/back_email";
    }
    /**
     * 发送邮件测试
     */
    @ResponseBody
    @RequestMapping("/test")
    public void test() {
	System.out.println("--------------->sendEmail");
	EmailHelper.sendEmail("853874947@qq.com", "庙里有座山</br>这是一封自动发送，占着仙气儿的邮件", "庙里有座山</br>这是一封自动发送，占着仙气儿的邮件");
    }
    /**
     * 后台-邮件管理-发送邮件
     * @return
     */
    @RequestMapping("/send")
    public String backEmailSend() {
	return "back/back_email_send";
    }
    /**
     * 发送邮件+保存邮件到数据库
     */
    @ResponseBody
    @RequestMapping("/sendEmail")
    public String backEmailSendEmail(HttpServletRequest request) {
	String emailAccount = request.getParameter("emailAccount");
	String emailSubject = request.getParameter("emailSubject");
	String emailContent = request.getParameter("emailContent");
	
	Map<String, String> map = new HashMap<String, String>();
	map.put("familyAccount", PrincipalFormat.currentFA());
	map.put("emailAccount", emailAccount);
	map.put("emailSubject", emailSubject);
	map.put("emailContent", emailContent);
	Email email = new Email();
	email.setId(UUID.randomUUID().toString());
	email.setEmail_family(PrincipalFormat.currentFA());
	email.setEmail_to(emailAccount);
	email.setEmail_subject(emailSubject);
	email.setEmail_content(emailContent);
	email.setCreated_time(DateFormat.dateFormate());
	email.setEmail_account("familyfinancial@163.com");
	emailService.addEmail(email);
	//发送邮件
	EmailHelper.sendEmail(emailAccount, emailSubject, emailContent);
	
	JSONObject jsonObject = new JSONObject();
	jsonObject.put("result", "success");
	return jsonObject.toJSONString();
    }
    /**
     * 后台-邮件管理-邮件查询
     */
    @ResponseBody
    @RequestMapping("/query")
    public String backEmailQuery(@RequestParam int pageNumber,int pageSize,String email_family,String email_to) {
	Map<String, String> map = new HashMap<String, String>();
    	map.put("email_family", email_family);
    	map.put("email_to", email_to);
    	int total = emailService.backEmailQuery(map).size();
    	PageHelper.startPage(pageNumber, pageSize);
    	List<Email> familyList = emailService.backEmailQuery(map);
	JSONObject jsonObject = new JSONObject();
	jsonObject.put("rows", familyList);
	jsonObject.put("total", total);
	return jsonObject.toJSONString();
    }
    /**
     * 后台-邮件管理-邮件删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    public String backEmailDelete(HttpServletRequest request) {
	Map<String, String> map = new HashMap<String, String>();
	map.put("id", request.getParameter("id"));
	emailService.backEmailDelete(map);
    	System.out.println("delete--------------");
    	JSONObject jsonObject = new JSONObject();
    	jsonObject.put("res", "success");
    	return jsonObject.toJSONString();
    } 
    @RequestMapping("/detail")
    public String backEmailDetailPage(@RequestParam String id,Model model) {
	System.out.println(id);
	Map<String, String> map = new HashMap<String, String>();
	map.put("id", id);
	List<Email> email = emailService.backEmailDetail(map);
	model.addAttribute("email", email);
    	return "back/back_email_detail";
    }
}

